package com.hmall.config;

import com.hmall.user.pojo.User;
import com.hmall.user.service.impl.UserService;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Random;

@Component
public class MyFeignInterceptor implements RequestInterceptor {

    @Autowired
    private UserService userService;

    @Override
    public void apply(RequestTemplate requestTemplate) {
        int count = userService.count();
        Long[] nums = new Long[count];
        int i = 0;
        for (User user : userService.list()) {
            nums[i++] = user.getId();
        }
        requestTemplate.header("authorization",nums[new Random().nextInt(count)].toString());
    }
}
